<!-- 讲师详情 -->
<template>
    <div class="app-container">
        <el-form ref="form" :model="teacher" label-width="80px">
            <el-form-item label="姓名" style="width:15%">
                <el-input v-model="teacher.name"></el-input>
            </el-form-item>
            <el-form-item label="头衔" style="width:15%">
                <el-select v-model="teacher.level" placeholder="头衔" clearable>
                    <el-option label="高级讲师" :value="1"></el-option>
                    <el-option label="首席讲师" :value="2"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="资历">
                <el-input
                    type="textarea"
                    :rows="4"
                    placeholder="请输入内容"
                    v-model="teacher.career"
                    style="width:40%"
                ></el-input>
            </el-form-item>
            <el-form-item label="简历">
                <div>
                    <tinymce v-model="teacher.intro" :height="300" :width="800" />
                </div>
            </el-form-item>
            <el-form-item label="头像">
                <pan-thumb :image="teacher.avatar" />
                <el-button
                    type="primary"
                    icon="el-icon-upload"
                    style="position: absolute;bottom: 15px;margin-left: 40px;"
                    @click="imagecropperShow = true"
                >Change Avatar</el-button>
                <image-cropper
                    v-show="imagecropperShow"
                    field="file"
                    :key="imagecropperKey"
                    :width="300"
                    :height="300"
                    :url="BASE_API"
                    lang-type="en"
                    @close="close"
                    @crop-upload-success="cropSuccess"
                />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="onSubmit">保存</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import { getById, updateTeacher, saveTeacher } from "@/api/ucenter/teacher";
import Tinymce from "@/components/Tinymce";
import ImageCropper from "@/components/ImageCropper";
import PanThumb from "@/components/PanThumb";

export default {
    //import引入的组件需要注入到对象中才能使用
    components: { Tinymce, ImageCropper, PanThumb },
    props: ["id"],
    data() {
        //这里存放数据
        return {
            teacher: {},
            imagecropperShow: false,
            imagecropperKey: 0,
            BASE_API: process.env.BASE_API + "/oss/uploadPhoto",
        };
    },
    //监听属性 类似于data概念
    computed: {},
    //监控data中的数据变化
    watch: {},
    //方法集合
    methods: {
        onSubmit() {
            this.$confirm("确认信息准确无误", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(() => {
                    if (this.id) {
                        updateTeacher(this.teacher).then((response) => {
                            this.$message({
                                message: "保存成功",
                                type: "success",
                            });
                        });
                    } else {
                        saveTeacher(this.teacher).then((response) => {
                            this.$message({
                                message: "添加成功",
                                type: "success",
                            });
                        });
                    }
                })
                .catch(() => {});
        },
        cropSuccess(resData) {
            this.imagecropperShow = false;
            this.imagecropperKey = this.imagecropperKey + 1;
            this.teacher.avatar = resData.url;
        },
        close() {
            this.imagecropperShow = false;
        },
    },
    //生命周期 - 创建完成（可以访问当前this实例）
    created() {},
    //生命周期 - 挂载完成（可以访问DOM元素）
    mounted() {
        if (this.id) {
            getById(this.id).then((response) => {
                this.teacher = response.data.teacher;
            });
        }
    },
    beforeCreate() {}, //生命周期 - 创建之前
    beforeMount() {}, //生命周期 - 挂载之前
    beforeUpdate() {}, //生命周期 - 更新之前
    updated() {}, //生命周期 - 更新之后
    beforeDestroy() {}, //生命周期 - 销毁之前
    destroyed() {}, //生命周期 - 销毁完成
    activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发，页面一旦激活就会触发
};
</script>
<style scoped>
</style>